package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mysql.SundayPayConfig;
import org.itboys.core.service.mysql.web.SundayPayConfigService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：圣代_支付_参数_controller_web
 */
@Controller
@RequestMapping("/sunday/web/pay")
public class SundayPayConfigController extends BaseController {
    @Autowired
    private SundayPayConfigService configService;

    /**
     * 主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/config/index")
    public String index(HttpServletRequest request,
                        Model model) {
        return "/sunday/pay/config/index";
    }


    /**
     * 查询积分配置
     *
     * @param request
     * @param response
     */
    @RequestMapping("/config/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        PageResult<SundayPayConfig> result = configService.selectConfig(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);

    }

    /**
     * 新增或者修改积分配置
     *
     * @param request
     * @param response
     * @param config
     */
    @RequestMapping("/config/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute SundayPayConfig config
    ) {
        configService.saveConfig(config);
        ResponseMessage.success();
    }


    @RequestMapping("/config/input")
    public String input(HttpServletRequest request,
                        Model model,
                        @RequestParam(value = "id", required = true) Long id,
                        @RequestParam(value = "type", required = false) Integer type) {
        SundayPayConfig payConfig = configService.findOne(id, type);
        model.addAttribute("config", payConfig);
        if (type == configService.pay_config_type_alipay) {
            return "/sunday/pay/config/input_alipay";
        } else if (type == configService.pay_config_type_wechat_open) {
            return "/sunday/pay/config/input_wechat_open";
        } else if (type == configService.pay_config_type_wechat_html) {
            return "/sunday/pay/config/input_wechat_html";
        }
        return "";
    }


}
